# -*- coding: utf-8 -*-


from lxml import etree  # xpath模块导入
import csv

"""
需求：获取a标签中的href属性值以及文本内容，并且将文本内容保存到csv文件当中
"""
# 模拟的是网页源代码
wb_data = """
        <div>
            <ul>
                 <li class="item-0"><a href="link1.html">first item</a></li>
                 <li class="item-1"><a href="link2.html">second item</a></li>
                 <li class="item-inactive"><a href="link3.html">third item</a></li>
                 <li class="item-1"><a href="link4.html">fourth item</a></li>
                 <li class="item-0"><a href="link5.html">fifth item</a>
             </ul>
         </div>
    """

# 1.转换
tree = etree.HTML(wb_data)  # tree == 整个源码数据

# 2. 获取数据
# 获取链接
# links = tree.xpath('//a/@href')
# # 获取文本
# data = tree.xpath('//a/text()')
# print(links,data)



a_lst = tree.xpath('//a')  # a_lst == a标签
"""
<a href="link1.html">first item</a>
<a href="link1.html">first item</a>
<a href="link1.html">first item</a>
<a href="link1.html">first item</a>
"""
# print(a_lst)
# 遍历每一个a标签里面的数据内容

# lst=[]
# for i in a_lst:  # 通过遍历之后  i == 5个a标签元素的
#     dic = {}
#     # print(i) # <Element a at 0x23ef8734e00> 代表的第一个a标签
#     href = i.xpath('@href')[0]
#     data = i.xpath('text()')[0]
#     # print(href,data)
#     # 字典添加内容
#     dic['链接'] = href
#     dic['文本'] = data # 操作的是字典(同一个对象)
#     lst.append(dic)
# print(lst)
#
# with open('测试.csv', 'w', encoding='utf-8-sig', newline='')as f:
#     # 创建csv对象
#     writer = csv.DictWriter(f, fieldnames=('链接', '文本'))
#     # 写入标头
#     writer.writeheader()
#     # 写入数据
#     writer.writerows(lst)



"""
链接              文本
link1.html      first item
link2.html      second item

保存csv文件
[{}{}{}]   [()()()]
"""



lst=[]
for i in a_lst:  # 通过遍历之后  i == 5个a标签元素的
    # print(i) # <Element a at 0x23ef8734e00> 代表的第一个a标签
    href = i.xpath('@href')[0]
    data = i.xpath('text()')[0]
    # print(href,data)
    # 字典添加内容
    tu = (href,data)
    lst.append(tu)
print(lst)

with open('测试.csv', 'w', encoding='utf-8-sig', newline='')as f:
    # 创建csv对象
    # writer = csv.DictWriter(f, fieldnames=('链接', '文本'))
    writer = csv.writer(f)
    # 写入标头
    writer.writerow(('链接','文本'))
    # 写入数据
    writer.writerows(lst)